package com.woniuxy.service.impl;

import com.woniuxy.dao.AuthDao;
import com.woniuxy.entity.User;
import com.woniuxy.entity.utils.ResponseEntity;
import com.woniuxy.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @Author: 马宇航
 * @Description: TODO
 * @DateTime: 22/12/09/0009 11:45
 * @Component: 成都蜗牛学苑
 **/
@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    AuthDao authDao;
    @Override
    @Cacheable(value = "user",key = "#user.getUsername()")
    public User findUser(User user) {
        return authDao.findByUsername(user.getUsername());
    }

    @Override
    @CachePut(value = "user",key = "#user.getUsername()")
    public User updateUser(User user) {
        return authDao.save(user);
    }

    @Override
    //allEntries为true就会清除所有的user::* 的缓存记录 key = "admin" 则无效
    @CacheEvict(value = "user",key = "admin",allEntries = true,beforeInvocation = true)
    public void clearCache() {
        int i = 1/0;//beforeInvocation设置为true，即使里面报错 也能清除所有缓存
    }
}
